import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ShapeDrawer extends JFrame {
    private DrawArea drawArea = new DrawArea();

    public ShapeDrawer() {
        this.setSize(400, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("图形绘制器");
        this.add(drawArea, BorderLayout.CENTER);

        // 修正后的选项（使用正确的中文字符）
        JComboBox<String> shapeSelector = new JComboBox<>(
                new String[] {"正方形", "长方形", "圆形", "三角形"}
        );

        this.add(shapeSelector, BorderLayout.NORTH);

        shapeSelector.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox) e.getSource();
                String shapeName = (String) cb.getSelectedItem();
                int size = 0; // 初始化尺寸变量
                boolean validInput = false; // 标记用户输入是否有效

                while (!validInput) { // 循环直到用户输入有效
                    String input = JOptionPane.showInputDialog(
                            "请输入" + shapeName + "的尺寸:"
                    );

                    if (input == null) { // 如果用户点击取消或关闭对话框，退出循环
                        return;
                    }

                    try {
                        size = Integer.parseInt(input); // 尝试将输入转换为整数
                        if (size > 0) { // 确保尺寸是正数
                            validInput = true; // 如果成功转换且为正数，标记输入有效并退出循环
                        } else {
                            JOptionPane.showMessageDialog(null, "输入无效，请输入一个正整数。"); // 提示用户输入正整数
                        }
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(null, "输入无效，请输入一个整数。"); // 如果失败，提示用户重新输入
                    }
                }

                drawArea.setShape(shapeName, size); // 更新图形和尺寸
            }

        });
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new ShapeDrawer().setVisible(true);
        });
    }
    //test
    private static class DrawArea extends JPanel {
        private String shape = "正方形"; // 设置默认形状
        private int size = 100; // 添加尺寸属性，默认为100

        public void setShape(String shape, int size) {
            this.shape = shape;
            this.size = size;
            this.repaint(); // 请求重新绘制面板
        }


        public class YourComponent extends JComponent {
            private static final String SHAPE_SQUARE = "正方形";
            private static final String SHAPE_RECTANGLE = "长方形";
            private static final String SHAPE_CIRCLE = "圆形";
            private static final String SHAPE_TRIANGLE = "三角形";

            // ... 其他代码 ...

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                switch (shape) {
                    case SHAPE_SQUARE:
                        g.drawRect(50, 50, size, size);
                        break;
                    case SHAPE_RECTANGLE:
                        g.drawRect(50, 50, size * 2, size);
                        break;
                    case SHAPE_CIRCLE:
                        g.drawOval(50, 50, size, size);
                        break;
                    case SHAPE_TRIANGLE:
                        int[] xPoints = {50 + size / 2, 50, 50 + size};
                        int[] yPoints = {50, 50 + size, 50 + size};
                        g.drawPolygon(xPoints, yPoints, 3);
                        break;
                    default:
                        break;
                }
            }

            // ... 其他代码 ...
        }

    }
}